package utils

import (
	"os"
	"path/filepath"
	"strings"
)

func GetFileListBySuffix(path string, suffix string, recursion bool) ([]string, error) {
	return GetFileList(path, "", func(fileName string) bool {
		return IsSuffix(fileName, suffix)
	}, recursion)
}

func GetFileList(rootPath, subDir string, filter func(fileName string) bool, recursion bool) ([]string, error) {
	files, err := os.ReadDir(filepath.Join(rootPath, subDir))
	if err != nil {
		return nil, err
	}
	var result []string
	for _, file := range files {
		if file.IsDir() && recursion {
			subfiles, err := GetFileList(rootPath, filepath.Join(subDir, file.Name()), filter, recursion)
			if err != nil {
				return nil, err
			}
			result = append(result, subfiles...)
		}
		if filter(file.Name()) {
			result = append(result, filepath.Join(subDir, file.Name()))
		}
	}
	return result, nil
}
func IsSuffix(fileName string, suffix string) bool {
	return suffix == filepath.Ext(fileName)
}
func GetFileNameWithoutExtension(filename string) string {
	ext := filepath.Ext(filename)
	return strings.TrimSuffix(filename, ext)
}
